package tq.com.tqteacher.message.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.message.adapter.TQNoticeAdapter;
import tq.com.tqteacher.message.fragment.TQMessageFragment;
import tq.com.tqteacher.message.model.TQNoticeModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQNoticeActivity extends BaseTitleActivity {

    @Override
    protected BaseFragment fragment() {
        return new TQNoticeFragment();
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getRightView().setText("新建公告");

        Drawable lDrawable = getDrawable(R.drawable.icon_add);
        lDrawable.setBounds(0, 0, lDrawable.getMinimumWidth(), lDrawable.getMinimumHeight());
        pHeader5View.getRightView().setCompoundDrawables(lDrawable, null, null, null);
        pHeader5View.getRightView().setCompoundDrawablePadding((int) getResources().getDisplayMetrics().density * 10);

        pHeader5View.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newNotice();
            }
        });


    }

    public void newNotice(){
        Intent intent = new Intent(this, TQNewNoticeActivity.class);
        startActivity(intent);
    }



    static public class TQNoticeFragment extends BaseRecyclerFragment{
        private TQNoticeAdapter mNoticeAdapter;
        private TQEventBus mEventBus;
        private int page = 1;

        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);

            if(mNoticeAdapter == null){
                mNoticeAdapter = new TQNoticeAdapter(getContext());
                mNoticeAdapter.setItemClickListener(mItemClickListener);
            }
            pRecyclerView.setAdapter(mNoticeAdapter);
            pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            pRecyclerView.setBackgroundColor(Color.parseColor("#f6f7f8"));

            if(mEventBus == null){
                mEventBus = new TQEventBus(mLisnter);
                mEventBus.register();
            }
        }

        @Override
        protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
            pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    page++;
                    loadData();
                }
            });

            pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    page = 1;
                    loadData();
                }
            });

            pRefreshLayout.autoRefresh();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mEventBus.unregister();
        }

        private TQNoticeAdapter.IItemClickListener mItemClickListener = new TQNoticeAdapter.IItemClickListener() {
            @Override
            public void clickItem() {
                gotoMsgDetail();
            }

            @Override
            public void deleteNotice(final TQNoticeModel pNoticeModel) {
                TQNetInterface.delNotice(pNoticeModel.getSeq_id(), new INetCallback() {
                    @Override
                    public void result(TQNetResult pNetResult) {
                        if(pNetResult.isSuccess()){
                            mNoticeAdapter.delNotice(pNoticeModel);
                        }else{
                            AlertDialogHelper.show(TQNoticeFragment.this.getContext(), pNetResult.getErrMsg());
                        }
                    }
                });
            }
        };

        public void gotoMsgDetail(){

        }

        private TQEventBus.IEventLisnter mLisnter = new TQEventBus.IEventLisnter() {
            @Override
            public void onEvnet(TQEventBus.TQEventMessage msg) {
                if(msg.getOperator() == TQEventBus.TQEventMessage.Opertor.add
                        && msg.getInfo() != null){
                    mNoticeAdapter.insertNotice(0, (TQNoticeModel) msg.getInfo());
                }
            }
        };

        private void loadData(){
            int cls = TQLogin.getCurrent().getUserInfo().getClzid();

            TQNetInterface.getNotice(cls, page, 20, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    boolean isRefresh = false;
                    if(mRefreshLayout.getState() == RefreshState.Loading){
                        mRefreshLayout.finishLoadMore();
                    }else{
                        isRefresh = true;
                        mRefreshLayout.finishRefresh();
                    }

                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQNoticeFragment.this.getContext(), pNetResult.getErrMsg());
                        return;
                    }

                    try {
                        JSONArray lArray = pNetResult.getData().getJSONArray("notice");
                        ArrayList<TQNoticeModel> lNoticeModels = new ArrayList<>();

                        for (int i = 0; i < lArray.length(); i++){
                            try {
                                JSONObject lObject = lArray.getJSONObject(i);

                                TQNoticeModel lNoticeModel = new TQNoticeModel();
                                lNoticeModel.refresh(lObject);

                                lNoticeModels.add(lNoticeModel);
                            }catch (JSONException pe){
                                pe.printStackTrace();
                            }
                        }

                        if(isRefresh){
                            mNoticeAdapter.replace(lNoticeModels);
                        }else{
                            mNoticeAdapter.append(lNoticeModels);
                        }


                    }catch (Exception pE){
                        pE.printStackTrace();
                    }
                }
            });
        }
    }
}
